package com.xhh.nphm.web.controller.system;

import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.system.domain.KbmCateM;
import com.xhh.nphm.system.domain.KbmPointM;
import com.xhh.nphm.system.domain.NodeCate;
import com.xhh.nphm.system.service.IKbmManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description: 知识库管理
 * @author: vich
 * @date: 2023/2/15 16:35
 * @version 1.0
 */
@Controller
@RequestMapping("/system/kbm")
public class KbmManageController extends BaseController {

    private final String prefix = "system/kbm";

    @Autowired
    IKbmManageService kbmManageService;

    /**
     * @Description: 进入类别管理页面
     * 2023/2/16
     * @Param: []
     * @return: org.springframework.web.servlet.ModelAndView
     */
    @GetMapping("/category/kbmCateList")
    public ModelAndView kbmCateList() {
        return new ModelAndView(prefix+"/category/kbmCateList");
    }

    /**
     * @Description: 加载类别列表数据
     * 2023/2/16
     * @Param: []
     * @return: com.xhh.nphm.common.core.page.TableDataInfo
     */
    @PostMapping("/category/kbmCateListJson")
    @ResponseBody
    public List<KbmCateM> kbmCateListJson(KbmCateM kbmCate) {
        return kbmManageService.kbmCateListJson(kbmCate);
    }


    /**
     * @Description: 打开新增类别页面
     * 2023/2/17
     * @Param: []
     * @return: org.springframework.web.servlet.ModelAndView
     */
    @GetMapping("/category/kbmCateAdd/{codePk}")
    public ModelAndView kbmCateAdd(@PathVariable("codePk")String codePk, ModelMap map) {
        KbmCateM kbmCateM = null;
        if(codePk.equals("1")){
            kbmCateM = new KbmCateM();//默认显示第一个
            kbmCateM.setCodepk("0");
            kbmCateM.setCodeparent("0");
            kbmCateM.setLayernum(-2);
            kbmCateM.setSortname("新类别");
        }else {
            kbmCateM = kbmManageService.selectCateById(codePk);
        }
        map.put("cate",kbmCateM);
        return new ModelAndView(prefix + "/category/kbmCateAdd");
    }

 
    @PostMapping("/category/kbmCateAdd")
    @ResponseBody
    public AjaxResult kbmCateAddSave(KbmCateM kbmCate){
        kbmCate.setUseflag(1);
        kbmCate.setIsdel(0);
        kbmCate.setBringtime(new Date());
        kbmCate.setFlagint(0);
        kbmCate.setFkint(0);
        return toAjax(kbmManageService.kbmCateAddSave(kbmCate));
    }


    /**
     * 校验类别编号
     */
    @PostMapping("/category/checkCodePkUnique")
    @ResponseBody
    public String checkCodePkUnique(KbmCateM kbmCate) {
        return kbmManageService.checkCodePkUnique(kbmCate);
    }

    /**
     * 校验类别名称
     */
    @PostMapping("/category/checkSortNameUnique")
    @ResponseBody
    public String checkSortNameUnique(KbmCateM kbmCate) {
        return kbmManageService.checkSortNameUnique(kbmCate);
    }

    /**
     * @Description: 加载类别列表树
     * 2023/2/21
     * @Param: []
     * @return: java.util.List<com.xhh.nphm.common.core.domain.Ztree>
     */
    @GetMapping("/category/cateTreeData")
    @ResponseBody
    public List<NodeCate> cateTreeData() {
        return kbmManageService.findCateList();
    }

    /**
     * 选择类别列表树
     */
    @GetMapping("/category/selectCateTree/{codePk}")
    public String selectCateTree(@PathVariable("codePk") String codePk, ModelMap map) {
        KbmCateM kbmCateM = null;
        if(codePk.equals("1")){
            kbmCateM = new KbmCateM();//默认显示第一个
            kbmCateM.setCodepk("0");
            kbmCateM.setCodeparent("0");
            kbmCateM.setLayernum(-2);
            kbmCateM.setSortname("新类别");
        }else {
            kbmCateM = kbmManageService.selectCateById(codePk);
        }
        map.put("cate", kbmCateM);
        return prefix + "/category/cateTree";
    }

    /**
     * 查询问题类别
     * @param request HttpServletRequest
     * @return
     */
    @RequestMapping("/category/kbmQuestionCate")
    @ResponseBody
    public Object findQuestionCate(HttpServletRequest request) {
        List<NodeCate> cateList = kbmManageService.findCateList();
        if (cateList == null || cateList.size() == 0) {
            return null;
        }
        return cateList.get(0).getChildren();
    }

    /**
     * 查询内容类别
     * @param request HttpServletRequest
     * @return
     */
    @RequestMapping("/category/kbmContentCate")
    @ResponseBody
    public Object findContentCate(HttpServletRequest request) {
        List<NodeCate> cateList = kbmManageService.findCateList();
        if (cateList == null || cateList.size() == 0) {
            return null;
        }
        return cateList.get(1).getChildren();
    }

    /**
     * @Description: 编辑类别
     * 2023/2/17
     * @Param: [codePk, map]
     * @return: java.lang.String
     */
    @GetMapping("/category/kbmCateEdit/{codePk}")
    public String kbmCateEdit(@PathVariable("codePk")String codePk,ModelMap map) {
        map.put("category",kbmManageService.findCateById(codePk));
        return prefix + "/category/kbmCateEdit";
    }
    
    /**
     * @Description: 保存编辑后的类别信息
     * 2023/2/20
     * @Param: [kbmCate]
     * @return: com.xhh.nphm.common.core.domain.AjaxResult
     */
    @PostMapping("/category/kbmCateEdit")
    @ResponseBody
    public AjaxResult kbmCateEditSave(KbmCateM kbmCate) {
        kbmCate.setBringtime(new Date());
        return toAjax(kbmManageService.kbmCateEditSave(kbmCate));
    }

    /**
     * @Description: 删除类别
     * 2023/2/22
     * @Param: [codePk]
     * @return: com.xhh.nphm.common.core.domain.AjaxResult
     */
    @GetMapping("/category/kbmCateRemove/{codePk}")
    @ResponseBody
    public AjaxResult kbmCateRemove(@PathVariable("codePk") String codePk){
        if (kbmManageService.selectPointByCodePK(codePk) > 0) {
            return AjaxResult.warn("该类别存在知识点,不允许删除");
        }
        if (kbmManageService.selectSonCateByCodePk(codePk) > 0) {
            return AjaxResult.warn("该类别存在子类别,不允许删除");
        }
        return toAjax(kbmManageService.removeCateByCodePk(codePk));
    }
    
    
    /**
     * @Description: 进入知识点管理页面
     * 2023/2/16
     * @Param: []
     * @return: org.springframework.web.servlet.ModelAndView
     */
    @GetMapping("/point/kbmPointList")
    public ModelAndView kbmPointList(ModelMap map) {
        List<NodeCate> children = kbmManageService.findCateList();
        if (children != null && children.size() != 0) {
            map.put("qCate", children.get(0).getChildren());
        } else {
            map.put("qCate",null);
        }
        return new ModelAndView(prefix + "/point/kbmPointList");
    }

    /**
     * @Description: 加载知识点列表数据
     * 2023/2/16
     * @Param: []
     * @return: com.xhh.nphm.common.core.page.TableDataInfo
     */
    @PostMapping("/point/kbmPointListJson")
    @ResponseBody
    public TableDataInfo kbmPointListJson(KbmPointM kbmPoint) {
        startPage();
        List<Map<String, Object>> list = kbmManageService.kbmPointListJson(kbmPoint);
        return getDataTable(list);
    }
    //TODO  新增知识点
    /**
     * @Description: 打开新增知识点页面
     * 2023/2/17
     * @Param: []
     * @return: org.springframework.web.servlet.ModelAndView
     */
    @GetMapping("/point/kbmPointAdd")
    public ModelAndView kbmPointAdd(ModelMap map) {
        List<NodeCate> children = kbmManageService.findCateList();
        if (children != null && children.size() != 0) {
            map.put("qCate", children.get(0).getChildren());
        } else {
            map.put("qCate",null);
        }
        return new ModelAndView(prefix + "/point/kbmPointAdd");
    }

    /**
     * @Description: 保存新增知识点
     * 2023/2/17
     * @Param: []
     * @return: org.springframework.web.servlet.ModelAndView
     */
    @PostMapping("/point/kbmPointAdd")
    @ResponseBody
    public AjaxResult kbmPointSAdd(@Validated KbmPointM kbmPoint) {
        kbmPoint.setDeptno(getSysUser().getDeptId().toString());
        kbmPoint.setDeptname(getSysUser().getDeptName());
        kbmPoint.setInputid(getSysUser().getUserNo());
        kbmPoint.setInputname(getSysUser().getUserName());
        kbmPoint.setInputtime(new Date());
        kbmPoint.setBringtime(new Date());
        kbmPoint.setTimessys(0);
        kbmPoint.setTimesweb(0);
        kbmPoint.setIsdel(0);
        return toAjax(kbmManageService.saveKbmPoint(kbmPoint));
    }

    /**
     * @Description: 编辑知识点
     * 2023/2/17
     * @Param: [codePk, map]
     * @return: java.lang.String
     */
    @GetMapping("/point/kbmPointEdit/{PkId}")
    public String kbmPointEdit(@PathVariable("PkId")String PkId,ModelMap map) {
        map.put("point",kbmManageService.findPointById(PkId));
        List<NodeCate> children = kbmManageService.findCateList();
        if (children.size() != 0 && children != null) {
            map.put("qCate", children.get(0).getChildren());
        }
        map.put("qCate",null);
        return prefix + "/point/kbmPointEdit";
    }

    /**
     * @Description: 保存编辑后的知识点
     * 2023/2/20
     * @Param: [kbmPoint]
     * @return: com.xhh.nphm.common.core.domain.AjaxResult
     */
    @PostMapping("/point/kbmPointEdit")
    @ResponseBody
    public AjaxResult kbmPointEditSave(KbmPointM kbmPoint) {
        kbmPoint.setModifyid(getSysUser().getUserNo());
        kbmPoint.setModifyname(getSysUser().getUserName());
        kbmPoint.setBringtime(new Date());
        return toAjax(kbmManageService.updateKbmPoint(kbmPoint));
    }
    
    /**
     * @Description: 修改知识点公开状态
     * 2023/2/20
     * @Param: [PkId]
     * @return: java.lang.String
     */
    @PostMapping("/point/changeStatus")
    @ResponseBody
    public AjaxResult kbmPointChangeStatus(KbmPointM kbmPoint) {
        return toAjax(kbmManageService.updateKbmPointStatus(kbmPoint));
    }
  
    /**
     * @Description: 删除知识点
     * 2023/2/20
     * @Param: [pkId]
     * @return: com.xhh.nphm.common.core.domain.AjaxResult
     */
    @PostMapping("/point/kbmPointRemove/{pkId}")
    @ResponseBody
    public AjaxResult kbmPointRemove(@PathVariable String pkId){
        return toAjax(kbmManageService.kbmPointRemove(pkId));
    }


}
